package com.open.demo.location

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener

/**
 * 高德定位服务Demo
 * @Author: hailong.qiu hailong.qiu@xgimi.com
 * @Maintainer: hailong.qiu hailong.qiu@xgimi.com
 * @Date: 2021/8/4
 * @Copyright: 2021 www.xgimi.com Inc. All rights reserved.
 */
class GaoDeActivity :AppCompatActivity(), AMapLocationListener {

    private var locationPermissionGranted = false
    private val locationClient by lazy {
        val client = AMapLocationClient(this)
        val option = AMapLocationClientOption()
        // 使用单次定位
        //        option.isOnceLocation = true
        // 地址信息
        option.isNeedAddress = true
        option.isLocationCacheEnable = true
        //
        client.setLocationOption(option)
        client.setLocationListener(this)
        client
    }

    companion object {
        const val TAG = "高德定位Demo"
        const val PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 250
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getLocationPermission()
        initLocationService()
    }

    private fun getLocationPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            locationPermissionGranted = true
        } else {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                GaoDeActivity.PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
            )
        }
    }

    private fun initLocationService() {
        if (locationPermissionGranted) {
            locationClient.startLocation()
        }
    }

    @SuppressLint("MissingSuperCall")
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        locationPermissionGranted = false
        when (requestCode) {
            GaoDeActivity.PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    locationPermissionGranted = true
                }
            }
        }
        //
        initLocationService()
    }

    override fun onLocationChanged(location: AMapLocation?) {
        Log.d(TAG, "onLocationChanged msg:${location?.getLocationStr()}")
    }

}

fun AMapLocation.getLocationStr():String {
    if (null != this) {
        if (this.errorCode == 0) {
            return "定位成功\n" +
                    "定位类型:${this.locationType}\n" +
                    "精   度:${this.longitude}\n" +
                    "维   度:${this.latitude}\n" +
                    "提供者 :${this.provider}\n" +
                    "海   拔:${this.altitude}\n" +
                    "速   度:${this.speed}\n" +
                    "角   度:${this.bearing}\n" +
                    "国   家:${this.country}\n" +
                    "省     :${this.province}\n" +
                    "市     :${this.city}\n" +
                    "城市编码:${this.cityCode}\n" +
                    "区     :${this.district}\n" +
                    "区域 码 :${this.adCode}\n" +
                    "地    址:${this.address}\n" +
                    "兴趣点   :${this.poiName}"
        } else {
            return "定位失败\n" +
                    "错误码:${this.errorCode}\n" +
                    "错误信息:${this.errorInfo}" +
                    "错误描述:${this.locationDetail}"
        }
    }
    return "定位AMapLocation为空"
}
